home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / KoUnit.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-30  |  6.5 KB  |  176 lines

  1. /* This file is part of the KDE project
  2.    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>, Torben Weis <weis@kde.org>
  3.    Copyright (C) 2004, Nicolas GOUTTE <goutte@kde.org>
  4.  
  5.    This library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This library is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public License
  16.    along with this library; see the file COPYING.LIB.  If not, write to
  17.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18.  * Boston, MA 02110-1301, USA.
  19. */
  20.  
  21. #ifndef kounit_h
  22. #define kounit_h
  23. #include <qstring.h>
  24. #include <qstringlist.h>
  25. #include <math.h> // for floor
  26. #include <koffice_export.h>
  27.  
  28. class KoXmlWriter;
  29. class QDomElement;
  30.  
  31. // 1 inch ^= 72 pt
  32. // 1 inch ^= 25.399956 mm (-pedantic ;p)
  33. // 1 pt = 1/12 pi
  34. // 1 pt ^= 0.0077880997 cc
  35. // 1 cc = 12 dd
  36. // Note: I don't use division but multiplication with the inverse value
  37. // because it's faster ;p (Werner)
  38. #define POINT_TO_MM(px) ((px)*0.352777167)
  39. #define MM_TO_POINT(mm) ((mm)*2.83465058)
  40. #define POINT_TO_CM(px) ((px)*0.0352777167)
  41. #define CM_TO_POINT(cm) ((cm)*28.3465058)
  42. #define POINT_TO_DM(px) ((px)*0.00352777167)
  43. #define DM_TO_POINT(dm) ((dm)*283.465058)
  44. #define POINT_TO_INCH(px) ((px)*0.01388888888889)
  45. #define INCH_TO_POINT(inch) ((inch)*72.0)
  46. #define MM_TO_INCH(mm) ((mm)*0.039370147)
  47. #define INCH_TO_MM(inch) ((inch)*25.399956)
  48. #define POINT_TO_PI(px)((px)*0.083333333)
  49. #define POINT_TO_DD(px)((px)*0.006490083)
  50. #define POINT_TO_CC(px)((px)*0.077880997)
  51. #define PI_TO_POINT(pi)((pi)*12)
  52. #define DD_TO_POINT(dd)((dd)*154.08124)
  53. #define CC_TO_POINT(cc)((cc)*12.840103)
  54. /**
  55.  * %KOffice stores everything in pt (using "double") internally.
  56.  * When displaying a value to the user, the value is converted to the user's unit
  57.  * of choice, and rounded to a reasonable precision to avoid 0.999999
  58.  */
  59. class KOFFICECORE_EXPORT KoUnit
  60. {
  61. public:
  62.     /** Length units supported by KOffice. */
  63.     enum Unit {
  64.         U_MM = 0,
  65.         U_PT = 1,
  66.         U_INCH = 2,
  67.         U_CM = 3,
  68.         U_DM = 4,
  69.         U_PI = 5, // pica
  70.         U_DD = 6, // didot
  71.         U_CC = 7, // cicero
  72.         U_LASTUNIT = U_CC // update when adding a new unit
  73.         // when adding a new unit, make sure to implement support for it in koRuler too
  74.     };
  75.  
  76.     /// Prepare ptValue to be displayed in pt
  77.     static double toPoint( double ptValue ) {
  78.         // No conversion, only rounding (to 0.001 precision)
  79.         return floor( ptValue * 1000.0 ) / 1000.0;
  80.     }
  81.  
  82.     /// Prepare ptValue to be displayed in mm
  83.     static double toMM( double ptValue ) {
  84.         // "mm" values are rounded to 0.0001 millimeters
  85.         return floor( POINT_TO_MM( ptValue ) * 10000.0 ) / 10000.0;
  86.     }
  87.  
  88.     /// Prepare ptValue to be displayed in cm
  89.     static double toCM( double ptValue ) {
  90.         return floor( POINT_TO_CM( ptValue ) * 10000.0 ) / 10000.0;
  91.     }
  92.  
  93.     /// Prepare ptValue to be displayed in dm
  94.     static double toDM( double ptValue ) {
  95.         return floor( POINT_TO_DM( ptValue ) * 10000.0 ) / 10000.0;
  96.     }
  97.  
  98.     /// Prepare ptValue to be displayed in inch
  99.     static double toInch( double ptValue ) {
  100.         // "in" values are rounded to 0.00001 inches
  101.         return floor( POINT_TO_INCH( ptValue ) * 100000.0 ) / 100000.0;
  102.     }
  103.  
  104.     /// Prepare ptValue to be displayed in pica
  105.     static double toPI( double ptValue ) {
  106.         // "pi" values are rounded to 0.00001 inches
  107.         return floor( POINT_TO_PI( ptValue ) * 100000.0 ) / 100000.0;
  108.     }
  109.  
  110.     /// Prepare ptValue to be displayed in didot
  111.     static double toDD( double ptValue ) {
  112.         // "dd" values are rounded to 0.00001 inches
  113.         return floor( POINT_TO_DD( ptValue ) * 100000.0 ) / 100000.0;
  114.     }
  115.  
  116.     /// Prepare ptValue to be displayed in cicero
  117.     static double toCC( double ptValue ) {
  118.         // "cc" values are rounded to 0.00001 inches
  119.         return floor( POINT_TO_CC( ptValue ) * 100000.0 ) / 100000.0;
  120.     }
  121.  
  122.     /**
  123.      * This method is the one to use to display a value in a dialog
  124.      * \return the value @p ptValue converted to @p unit and rounded, ready to be displayed
  125.      * Old name: ptToUnit
  126.      */
  127.     static double toUserValue( double ptValue, Unit unit );
  128.  
  129.     /**
  130.      * Convert the value @p ptValue to a given unit @p unit
  131.      * Unlike KoUnit::ptToUnit the return value remains unrounded, so that it can be used in complex calculation
  132.      * \return the converted value
  133.      * Old name: ptToUnitUnrounded
  134.      */
  135.     static double ptToUnit( const double ptValue, const Unit unit );
  136.  
  137.     /// This method is the one to use to display a value in a dialog
  138.     /// @return the value @p ptValue converted to @p unit and rounded, ready to be displayed
  139.     /// Old name: userValue
  140.     static QString toUserStringValue( double ptValue, Unit unit );
  141.  
  142.     /// This method is the one to use to read a value from a dialog
  143.     /// @return the value in @p unit, converted to points for internal use
  144.     /// Old name: ptFromUnit
  145.     static double fromUserValue( double value, Unit unit );
  146.  
  147.     /// This method is the one to use to read a value from a dialog
  148.     /// @param value value entered by the user
  149.     /// @param unit unit type selected by the user
  150.     /// @param ok if set, the pointed bool is set to true if the value could be
  151.     /// converted to a double, and to false otherwise.
  152.     /// @return the value in @p unit, converted to points for internal use
  153.     static double fromUserValue( const QString& value, Unit unit, bool* ok = 0 );
  154.  
  155.     /// Convert a unit name into a Unit enum
  156.     /// @param _unitName name to convert
  157.     /// @param ok if set, it will be true if the unit was known, false if unknown
  158.     static Unit unit( const QString &_unitName, bool* ok = 0 );
  159.     /// Get the name of a unit
  160.     static QString unitName( Unit _unit );
  161.     /// Get the full (translated) description of a unit
  162.     static QString unitDescription( Unit _unit );
  163.     static QStringList listOfUnitName();
  164.  
  165.     /// parse common %KOffice and OO values, like "10cm", "5mm" to pt
  166.     static double parseValue( QString value, double defaultVal = 0.0 );
  167.     // Note: the above method doesn't take a const ref, since it modifies the arg.
  168.  
  169.     /// Save a unit in OASIS format
  170.     static void saveOasis(KoXmlWriter* settingsWriter, Unit _unit);
  171.  
  172. };
  173.  
  174.  
  175. #endif
  176.